package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.BorrowQueryAdapter
import com.ht.medicaltreatment.bean.BorrowQueryPage
import com.ht.medicaltreatment.viewmodel.BorrowBuildViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.borrow_query_layout.*

//借出 查询 添加
class BorrowQueryActivity : BaseActivity(), OnItemClickListener {
    override fun getLayoutId() = R.layout.borrow_query_layout

    private val model by viewModels<BorrowBuildViewModel>()
    private lateinit var itemAdapter: BorrowQueryAdapter
    private var currentPage = 1
    private lateinit var projectId:String
    private lateinit var loanOrderId:String
    private lateinit var sign:String
    private lateinit var name:String
    private lateinit var sampleCategoryDictId:String

    override fun init() {
        setTitle("查询内容")
        projectId = intent.getStringExtra("projectId")?:"-1"
        name = intent.getStringExtra("sampleName")?:""
        sampleCategoryDictId = intent.getStringExtra("sampleCategoryId")?:""
        loanOrderId = intent.getStringExtra("orderId")?:""
        sign = intent.getStringExtra("type")?:""
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = BorrowQueryAdapter(sign).apply { itemAdapter = this }

        if(sign == "0"){
            titleCheck.text = "已添加样本数量"
        }else{
            titleCheck.text = "已添加样本盒数量"
        }

        refreshLayout.setOnLoadMoreListener {
            currentPage++
            initData()
        }

        initClick()
        showProgress()
        initData()
    }

    fun initData(){
        model.query(currentPage,loanOrderId,name,projectId,sampleCategoryDictId,sign,{
            dismissProgress()
            if(currentPage > 1){
                refreshLayout.finishLoadMore()
                itemAdapter.addData(it.pageList)
            }else{
                itemAdapter.setNewInstance(it.pageList)
            }

            tvCheckCOunt.text = "${it.totalNumber}"
        })
    }

    private fun initClick() {
        //添加
        btnAdd.setOnClickListener {
            val list = itemAdapter.data.filter { it.isChecked }.apply { forEach { it.isChecked = false } }
            if(list.size == 0)return@setOnClickListener

            val idsSB = StringBuffer()
            val rfidsSB = StringBuffer()
            list.forEach {
                idsSB.append("${it.id},")
                rfidsSB.append("${it.rfidCode},")
            }
            var ids = idsSB.toString().substring(0,idsSB.toString().length - 1)
            var rfids = rfidsSB.toString().substring(0,rfidsSB.toString().length - 1)
            LogUtils.i(ids,rfids,loanOrderId)
            showProgress()
            if(sign == "0"){
                //样本
                model.addSample(ids,loanOrderId,rfids,{
                    dismissProgress()
                    showToast("添加成功")
                    setResult(RESULT_OK,Intent())
                    finish()
                })
            }else{
                model.addSampleBox(ids,loanOrderId,projectId,{
                    dismissProgress()
                    showToast("添加成功")
                    setResult(RESULT_OK, Intent())
                    finish()
                })
            }
            //val intent = Intent()
            //intent.putExtra("json",json)

        }
        cbAll.setOnClickListener{
            itemAdapter.data.forEach {
                it.isChecked = cbAll.isChecked
            }
            if(cbAll.isChecked){
                tvCheckCount.text = "${itemAdapter.data.size}"
            }else{
                tvCheckCount.text = "0"
            }
            itemAdapter.notifyDataSetChanged()
        }

        itemAdapter.setOnItemClickListener(this)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as BorrowQueryPage
        item.isChecked = !item.isChecked
        adapter.notifyItemChanged(position)
        val list = adapter.data as MutableList<BorrowQueryPage>
        val bean = list.find { !it.isChecked }
        if(bean == null){
            cbAll.isChecked = true
        }else{
            if(cbAll.isChecked){
                cbAll.isChecked = false
            }
        }
        tvCheckCount.text = "${list.filter { it.isChecked }.size}"
    }
}
